/*
 *	jquery.rate 1.0 - 2009-09-29
 *
 *	All the stuff written by pwwang (pwwang.com)	
 *	Feel free to do whatever you want with this file
 *
 */
(function($) {
	$.rate = function(wraper, options) {
		var $wraper = $(wraper);
		var $rateUnits = [];
		for(var i=0; i<options.rateMax; i++){	
			$rateUnits[i] =  $(document.createElement("a"))	.attr("href","javascript:;").addClass(options.rateClass);						
			if( i<options.rated ) $rateUnits[i].addClass(options.ratedClass);
			$wraper.append($rateUnits[i]);
		}
		$.each($rateUnits, function(){						 
			$(this).hover(						  
				function(){ $(this).prevAll().add($(this)).addClass(options.rateOverClass) },				
				function(){ $(this).prevAll().add($(this)).removeClass(options.rateOverClass) }	
			);			
			$(this).click(function(){
				var index = $wraper.children().index($(this));	
				if(options.ratePage){
					$.get(options.ratePage, { rate:index+1 }, function(data){ (options.rateAfterEvent)(data);	});
				}
				$(this).prevAll().add($(this)).addClass(options.ratedClass);
				$(this).nextAll().removeClass(options.ratedClass);
				if(typeof(options.onrate)=="function"){
					options.onrate(index+1);
				}
			});	
		});	
	}	
	$.fn.rate = function(options) {
		options 				= options || {};
		options.rated 			= 3;
		options.rateMax 		= options.rateMax || 5;
		options.rateClass 		= options.rateClass || "star";
		options.rateOverClass 	= options.rateOverClass || "star_on";
		options.ratedClass 		= options.ratedClass || "star_yes"
		options.rateImg 		= options.rateImg || "images/star.gif";
		options.rateAfterEvent 	= options.rateAfterEvent || function(){};
		options.ratePage		= options.ratePage || "rate.php";
		$.rate(this, options);
		return this;	
	};	
})(jQuery);